package ui.tui;

import java.util.Scanner;
/**
 * @autor Piesold
 */
public class Keyboard {

// read => logical
	/**
	 * reads a <code>Boolean</code>
	 * @return <code>Boolean</code>
	 */
	public static boolean readBoolean(){
		boolean p = false;
		boolean b = true;
		String t = "";

		while(b){
			t = read();
			if(t.equalsIgnoreCase("true") ||t.equalsIgnoreCase("false") ||t.equalsIgnoreCase("1") ||t.equalsIgnoreCase("0")){
				if(t.equalsIgnoreCase("true") ||t.equalsIgnoreCase("1")){
					p = true;
				}else{
					p = false;
				}
				b = false;
			}else{
				throwError(getErrorText(t, "Boolean"));
			}
		}

		return p;
	}

// read => number
	/**
	 * reads a <code>Byte</code>
	 * @return <code>Byte</code>
	 */
	public static byte readByte(){
		byte p = 0;
		boolean b = true;
		String t = "";

		while(b){
			try {
				t = read();
				p = Byte.parseByte(t);
				b = false;
			}catch (Exception e) {throwError(getErrorText(t, "Byte"));}
		}

		return p;
	}
	/**
	 * read a <code>Short</code>
	 * @return <code>Short</code>
	 */
	public static short readShort(){
		short p = 0;
		boolean b = true;
		String t = "";

		while(b){
			try {
				t = read();
				p = Short.parseShort(t);
				b = false;
			}catch (Exception e) {throwError(getErrorText(t, "Short"));}
		}

		return p;
	}
	/**
	 * read an <code>Integer</code>
	 * @return <code>Integer</code>
	 */
	public static int readInt(){
		int p = 0;
		boolean b = true;
		String t = "";

		while(b){
			try {
				t = read();
				p = Integer.parseInt(t);
				b = false;
			}catch (Exception e) {throwError(getErrorText(t, "Integer"));}
		}

		return p;
	}
	/**
	 * read a <code>Long</code>
	 * @return <code>Long</code>
	 */
	public static long readLong(){
		long p = 0;
		boolean b = true;
		String t = "";

		while(b){
			try {
				t = read();
				p = Long.parseLong(t);
				b = false;
			}catch (Exception e) {throwError(getErrorText(t, "Long"));}
		}

		return p;
	}

// read => floating point number
	/**
	 * read a <code>Float</code>
	 * @return <code>Float</code>
	 */
	public static float readFloat(){
		float p = 0;
		boolean b = true;
		String t = "";

		while(b){
			try {
				t = read();
				p = Float.parseFloat(t);
				b = false;
			}catch (Exception e) {throwError(getErrorText(t, "Float"));}
		}

		return p;
	}
	/**
	 * read a <code>Double</code>
	 * @return <code>Double</code>
	 */
	public static double readDouble(){
		double p = 0;
		boolean b = true;
		String t = "";

		while(b){
			try {
				t = read();
				p = Double.parseDouble(t);
				b = false;
			}catch (Exception e) {throwError(getErrorText(t, "Double"));}
		}

		return p;
	}

// read => text
	/**
	 * read a <code>Character</code>
	 * @return <code>Character</code>
	 */
	public static char readChar(){
		char p = ' ';
		boolean b = true;
		String t = "";

		while(b){
			t = read();
			if(t.length() == 1){
				b = false;
				p = t.charAt(0);
			}else{
				throwError(getErrorText(t, "Character"));
			}
		}

		return p;
	}
	/**
	 * read a <code>String</code>
	 * @return <code>String</code>
	 */
	public static String readString(){
		return read();
	}

	/**
	 *
	 * @param var the readed value
	 * @param datatype the datatyp it should be
	 * @return String the error text
	 */
	private static String getErrorText(String var, String datatype){
		StringBuffer sb = new StringBuffer();

		sb.append("[").append(var).append("]")
		  .append(" is not a");

		if( (datatype.charAt(0) == 'a') || (datatype.charAt(0) == 'e') || (datatype.charAt(0) == 'i') || (datatype.charAt(0) == 'o') || (datatype.charAt(0) == 'u') ||
			(datatype.charAt(0) == 'A') || (datatype.charAt(0) == 'E') || (datatype.charAt(0) == 'I') || (datatype.charAt(0) == 'O') || (datatype.charAt(0) == 'U')){
			sb.append("n");
		}

		sb.append(" ").append(datatype).append("!");

		return sb.toString();
	}

	/**
	 * uses the System.err strem to show the given error message
	 * @param etext the message to show
	 */
	private static void throwError(String etext){
		System.err.println(etext);
	}

	/**
	 * uses the System.in strem to read user inputs
	 * @return String the input from user
	 */
	private static String read(){
		return new Scanner(System.in).nextLine();
	}



}